package com.bosssoft.hr.jobmonitor.core.file;

import com.bosssoft.hr.jobmonitor.config.common.SysConfig;
import com.bosssoft.hr.jobmonitor.core.auth.AuthInterceptor;
import com.bosssoft.hr.jobmonitor.util.DateUtil;
import com.bosssoft.hr.jobmonitor.util.LoggerUtil;
import com.bosssoft.hr.jobmonitor.util.XmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;

import java.io.*;
import java.net.URLDecoder;
import java.util.Date;
/***
 * @Author lys(cherish)
 * @Description 文件处理工具
 * @Date  2019/2/26
 **/
public class FileTool {

    private static final Logger logger= LoggerFactory.getLogger(AuthInterceptor.class);

    /***
     * @Author lys(cherish)
     * @Description 读取文件信息
     * @Date  2019/2/26
     * @Param [filename] 文件名称
     * @return java.lang.String 文件内容
     **/
    public String readFile(String filename){
        BufferedReader br=null;
        StringBuilder stringBuilder=new StringBuilder();
        InputStream in=null;
        Resource resource =new ClassPathResource(filename);
        try {
            in = resource.getInputStream();
        } catch (IOException e) {
            logger.info(LoggerUtil.fmtLog("读文件出现流获取异常",e));
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
        try {
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                stringBuilder.append(str);
            }
        }catch (Exception e){
            logger.info(LoggerUtil.fmtLog("读文件 bufferReader 出现异常",e));
        }
        return stringBuilder.toString();
    }

    /***
     * @Author lys(cherish)
     * @Description 将字符串写入文件中
     * @Date  2019/2/26
     * @Param [filename, xmlStr] 文件名称（含路径）、字符串
     * @return void
     **/
    public void writeFile(String filename,String xmlStr){
        OutputStream outputStream=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        String basePatha1 = ClassUtils.getDefaultClassLoader().getResource(filename).getPath();
        try {
            basePatha1 = URLDecoder.decode(basePatha1, "utf-8");
        } catch (UnsupportedEncodingException e) {
            logger.info(LoggerUtil.fmtLog("写文件操作编码异常",e));
        }
        try {
            outputStream = new FileOutputStream(basePatha1);
        } catch (IOException e) {
            logger.info(LoggerUtil.fmtLog("写文件操作出现流异常",e));
        }
        try {
            osw = new OutputStreamWriter(outputStream);
            bw = new BufferedWriter(osw);
            bw.write(xmlStr);
        } catch (IOException e) {
            logger.info(LoggerUtil.fmtLog("写文件出现异常",e));
        }finally {
            try {
                bw.close();
                osw.close();
                outputStream.close();
            } catch (IOException e) {
                logger.info(LoggerUtil.fmtLog("写文件操作结束后的关流异常",e));
            }
        }
    }

    /***
     * @Author lys(cherish)
     * @Description 获取系统通用配置
     * @Date  2019/2/26
     * @Param []
     * @return com.bosssoft.hr.jobmonitor.config.common.SysConfig
     **/
    public SysConfig getSysConfig(){
        String xmlStr = readFile("resource"+File.separator+"common"+File.separator+"sysConfig.xml");
        SysConfig sysConfig = XmlUtil.toBean(xmlStr, SysConfig.class);
        return sysConfig;
    }

    /***
     * @Author lys(cherish)
     * @Description 更新系统配置
     * @Date  2019/2/26
     * @Param []
     * @return void
     **/
    public void updateSysConfig(){
        SysConfig sysConfig = getSysConfig();
        String newXmlStr = XmlUtil.toXml(sysConfig);
        writeFile( "resource"+File.separator+"common"+File.separator+"sysConfig.xml",newXmlStr);
    }

    /***
     * @Author lys(cherish)
     * @Description 在作业目录创建作业日期文件夹
     * @Date  2019/2/26
     * @Param []
     * @return void
     **/
    public void makeHomeworkDir() {
        String todayDir = DateUtil.getTodayDir(new Date());
        SysConfig sysconfig = getSysConfig();
        File file = new File(sysconfig.getHomeDir() + File.separator + todayDir);
        file.mkdirs();
    }
}
